package com.kye.express.business.deposit_recharge.online;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.deposit.RequestJudgeLegalMoney;
import com.kye.model.bean.deposit.ResultJudgeLegalMoneyBean;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.DepositDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 预存运费-充值（线下） Presenter
 * <p>
 * Created by SkyYu on 2018/1/20
 */
public interface RechargeOnlineContract {

    interface RechargeOnlineView extends IView {
        void onLoadUserInfo(User user);

        void onJudgeLegalMoneySuccess(OpenApiResult<ResultJudgeLegalMoneyBean> bean);

        void onJudgeLegalMoneyApiError(OpenApiResult<ResultJudgeLegalMoneyBean> result, int code, String msg);

        void onJudgeLegalMoneyOtherError(Throwable e);
    }

    class RechargeOnlinePresenter extends BasePresenter<RechargeOnlineView> {

        DepositDataSource dataSource;

        RechargeOnlinePresenter() {
            dataSource = new DepositDataSource();
        }

        @Override
        public void bind(RechargeOnlineView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((user) ->
                            getView().onLoadUserInfo(user)
                    );
        }

        void requestToJudgeLegalMoney(String money) {
            dataSource.judgeLegalMoney(new RequestJudgeLegalMoney(money))
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<ResultJudgeLegalMoneyBean>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onSuccess(OpenApiResult<ResultJudgeLegalMoneyBean> result, ResultJudgeLegalMoneyBean resultJudgeLegalMoneyBean) {
                            getView().onJudgeLegalMoneySuccess(result);
                        }

                        @Override
                        public void onApiError(OpenApiResult<ResultJudgeLegalMoneyBean> result, int code, String msg) {
                            getView().onJudgeLegalMoneyApiError(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onJudgeLegalMoneyOtherError(e);
                        }
                    });
        }
    }
}